/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-05-04 09:13:14
 * @LastEditTime : 2023-07-10 17:14:28
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Router\MenuNav\Common\options.jsx
 * @Description  :
 */
import { useBoolean, useEventEmitter, useRequest } from 'ahooks'
import { Button, message, Dropdown } from 'antd'
import { Http } from '../../../Api'
import { WeChatMenuItem } from '../../../Configure/WeChatMenuItem'
import { Mars3DModal } from '../../../Mars3d/Modal'
import { ForgeModal } from '../../../Forge/Modal'
import { ForgeBox } from '../../../Scenes/Forge/ForgeProgress'
import { ExportExcel } from '../../../Configure/QRcode'
import { ModalBox } from '../../../Modal/ModalBox'
import { useState } from 'react'
import { saveAs } from 'file-saver'

import { PlusOutlined, DownOutlined, FileSearchOutlined, CopyOutlined, CloudDownloadOutlined } from '@ant-design/icons'

// *** 列表更多操作按钮
export const MoreActionsButton = ({ title, items, data, type }) => {
    const [messageApi, contextHolder] = message.useMessage()
    const { runAsync } = useRequest(Http, { manual: true })
    const [OptionsCall, setOptionsCall] = useState()
    const ModalEvent = useEventEmitter()
    ModalEvent.useSubscription(val => {
        const { url, path, tips, actionRef } = val.data
        exeFunc({ url, path, tips, actionRef })
    })
    const exeFunc = ({ url, params, path, tips, actionRef }) => {
        runAsync({ url, data: params, path }).then(() => {
            messageApi.success(tips)
            actionRef.current.reload()
        })
    }
    const click = ({ key }) => {
        const { record, OptionsEvent, header, Permission, actionRef } = data
        const { type, api, modal, smallTitle, url, tips, path, params, fun, classification, width } = data[key]
        switch (type) {
            case 'ExportExcel':
                // 导出excel
                return ExportExcel(api, record)
            case 'SelectExcel':
                // 导出带条件excel
                OptionsEvent.emit({ type: 'SelectExcel', data: { api } })
                break
            case 'OpenExcel':
                // 打开excel
                OptionsEvent.emit({ type: 'OpenExcel', data: { title: smallTitle, width: 1500, data: data[key] } })
                break
            case 'OpenTwoRouter':
                // 二级路由
                OptionsEvent.emit({
                    type: 'TwoRouter',
                    data: {
                        title: smallTitle,
                        modal,
                        width: 1500,
                        other: { page: key, Permission, oneData: record, header },
                    },
                })
                break
            case 'ExecuteButton':
                // 执行函数
                if (fun === 'delete')
                    setOptionsCall({
                        type: 'TipsModal',
                        data: { title: smallTitle, data: { url, path, tips, actionRef } },
                    })
                else exeFunc({ url, params, path, tips, actionRef })
                break
            case 'openModel':
                // 打开弹出层
                OptionsEvent.emit({
                    type: key,
                    data: { title: smallTitle, record, header, Permission, width: '80vw', actionRef },
                })
                break
            case 'openCommon':
                OptionsEvent.emit({
                    type: modal,
                    data: { title: smallTitle, data: { record, classification }, width: width || 1000 },
                })
                break
            case 'openAnnex':
                OptionsEvent.emit({ type: 'Annex', data: { url, title, width: '90vw' } })
                break

            case 'BusinessCard':
                setOptionsCall({
                    type: 'BusinessCard',
                    data: { title: '个人名片', data: record, width: 800 },
                })
                break
            default:
                break
        }
    }

    return (
        <>
            <Dropdown menu={{ items, onClick: click }} trigger={['click']}>
                <Button type={type || 'link'}>
                    {title}
                    <DownOutlined />
                    {contextHolder}
                </Button>
            </Dropdown>
            <ModalBox OptionsCall={OptionsCall} ModalEvent={ModalEvent} />
        </>
    )
}

const ButtonConfig = ({ type, danger, icon, title, click }) => {
    const iconList = {
        add: <PlusOutlined />,
        a01: <FileSearchOutlined />,
        a02: <CopyOutlined />,
        a03: <CloudDownloadOutlined />,
    }
    return (
        <Button type={type || 'link'} danger={danger} onClick={click} icon={iconList[icon] || ''}>
            {title || '操作'}
        </Button>
    )
}

// 新增按钮
export const AddDataButton = ({ OptionsEvent, type, icon, title, AddTo, Adduuid }) => {
    const click = () => OptionsEvent.emit({ type: 'Add', data: { title, AddTo, Adduuid } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}
// 修改按钮
export const ModDataButton = ({ OptionsEvent, type, icon, data, title }) => {
    const click = () => OptionsEvent.emit({ type: 'Mod', data: { title, data } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}
// 删除按钮
export const DelDataButton = ({ OptionsEvent, type, icon, url, title, uuid, way }) => {
    const click = () => OptionsEvent.emit({ type: 'Del', data: { url, title, uuid, way } })
    return <ButtonConfig title={title} type={type} danger={true} icon={icon} click={click} />
}
// 查看附件
export const AnnexButton = ({ OptionsEvent, type, icon, url, title, readonly }) => {
    const click = () => (!readonly ? OptionsEvent.emit({ type: 'Annex', data: { url, title, width: '90vw' } }) : '')
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}
// 打印二维码
export const PrintQRCodeButton = ({ OptionsEvent, type, icon, title, data, url }) => {
    const click = () => {
        OptionsEvent.emit({ type: 'PrintQRcode', data: { title, width: 800, data, url } })
    }
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}
// 普通二维码查看下载
export const OrdinaryQRCodeButton = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => OptionsEvent.emit({ type: 'OrdinaryQRCode', data: { title, width: 600, data } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 下拉选择框
export const SelectButton = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => OptionsEvent.emit({ type: 'SelectModal', data: { title, data, width: 800 } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 上传操作
export const UploadOperationBtn = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => OptionsEvent.emit({ type: 'UploadOperation', data: { title, data, width: 800 } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 下载EXCEL
export const SaveExcelButton = ({ type, icon, title, api, data }) => {
    const click = () => ExportExcel(api, data)
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 下载附件
export const SaveFilesUrl = ({ type, icon, title, url, originalName }) => {
    const click = () => saveAs(url, originalName)
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 带搜索条件的Excel
export const SelectExcelButton = ({ OptionsEvent, type, icon, title, api, data }) => {
    const click = () => OptionsEvent.emit({ type: 'SelectExcel', data: { api, data } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 打开EXCEL
export const OpenExcelButton = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => OptionsEvent.emit({ type: 'OpenExcel', data: { title, width: '98vw', data } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}
// 监控画面
export const MonitorVideoButton = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => OptionsEvent.emit({ type: 'MonitorVideo', data: { title, width: 1200, data } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 打开试验详情
export const TestDetailsButton = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () =>
        OptionsEvent.emit({ type: data.type, data: { data: data.data, title: data.title, width: 1200 } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 选择 - 上传数据按钮
export const SynchronizeButton = ({ OptionsEvent, type, icon, title, smallTitle, data }) => {
    const click = () => {
        OptionsEvent.emit({ type: 'SynchronizeScore', data: { title: smallTitle, data } })
    }
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 张拉压浆匹配窗口
export const TAPMateButton = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => OptionsEvent.emit({ type: 'TAPMate', data: { data, title: data.smallTitle } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}
// Input 输入框
export const InputButton = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => OptionsEvent.emit({ type: 'InputModal', data: { data, title: data.smallTitle } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 打开二级路由
export const OpenTwoRouter = ({ OptionsEvent, type, icon, title, twoTitle, modal, other }) => {
    const click = () =>
        OptionsEvent.emit({ type: 'TwoRouter', data: { title: twoTitle || title, modal, other, width: 1500 } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}
// 打开三级路由
export const OpenThrTouter = ({ OptionsEvent, type, icon, title, twoTitle, modal, other, width }) => {
    const click = () =>
        OptionsEvent.emit({ type: 'ThrRouter', data: { title: twoTitle || title, modal, other, width: width || 800 } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 小程序页面配置
export const WeChatPageSet = ({ type, icon, data, title }) => {
    const [ModalStatus, { setTrue, setFalse }] = useBoolean(false)
    return (
        <>
            <ButtonConfig title={title} type={type} icon={icon} click={setTrue} />
            {ModalStatus && <WeChatMenuItem Off={setFalse} data={data} />}
        </>
    )
}
// 执行接口按钮
export const ExecuteButton = ({ type, icon, title, url, actionRef, danger, data, path, tips, fun }) => {
    const [messageApi, contextHolder] = message.useMessage()
    const { runAsync } = useRequest(Http, {
        manual: true,
        onSuccess: () => {
            messageApi.success(tips)
            actionRef.current.reload()
        },
    })
    const [OptionsCall, setOptionsCall] = useState()
    const ModalEvent = useEventEmitter()
    ModalEvent.useSubscription(val => {
        runAsync({ url, data, path })

        console.log(url, data, path)
    })
    const click = () => {
        if (fun === 'delete') setOptionsCall({ type: 'TipsModal', data: { title, data: {} } })
        else runAsync({ url, data, path })
    }

    return (
        <>
            {contextHolder}
            <ButtonConfig title={title} type={type} icon={icon} danger={danger} click={click} />
            <ModalBox OptionsCall={OptionsCall} ModalEvent={ModalEvent} />
        </>
    )
}
// 选项式审核操作
export const ExamineButton = ({ title, icon, type, danger, data, options }) => {
    const MultipleEvent = useEventEmitter()
    const { runAsync } = useRequest(Http, { manual: true })
    const { actionRef, OptionsEvent, url, smallTitle } = data
    const click = () => {
        OptionsEvent.emit({ type: 'Multiple', data: { title: smallTitle, MultipleEvent, options } })
    }
    MultipleEvent.useSubscription(({ emit, Off }) => {
        const { data, path } = emit
        runAsync({ url, data, path }).then(() => reloadFunc({ Off }))
    })
    const reloadFunc = ({ Off }) => {
        actionRef.current.reload()
        Off()
    }
    return <ButtonConfig title={title} type={type} icon={icon} danger={danger} click={click} />
}

// 添加式审核操作
export const ExamineFillButton = ({ OptionsEvent, title, icon, type, data }) => {
    const click = () => OptionsEvent.emit({ type: 'LabHandling', data: { title, data } })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 打开场景弹出层
export const OpenScenesButton = ({ actionRef, type, icon, title, data }) => {
    const [Mars3DStatus, { setTrue: Mars3DTrue, setFalse: Mars3DFalse }] = useBoolean(false)
    const [ForgeStatus, { setTrue: ForgeTrue, setFalse: ForgeFalse }] = useBoolean(false)

    const SceneList = {
        'bb84e25e-1b42-44b5-bae8-7d8c7eb44814': () => Mars3DTrue(),
        'a3d3ab74-3301-454d-becc-20cb15483295': () => ForgeTrue(),
    }

    return (
        <>
            <ButtonConfig title={title} type={type} icon={icon} click={SceneList[data.category]} />
            {Mars3DStatus && (
                <Mars3DModal
                    Off={Mars3DFalse}
                    data={{ title: 'Mars3D场景明细', scene: data }}
                    initializationEvent={actionRef}
                />
            )}
            {ForgeStatus && (
                <ForgeModal
                    Off={ForgeFalse}
                    data={{ title: 'APS场景明细', scene: data }}
                    initializationEvent={actionRef}
                />
            )}
        </>
    )
}

// 打开APS进度
export const ForgeProgressWindowBtn = ({ title, type, data }) => {
    const [ModalState, { setTrue, setFalse }] = useBoolean(false)
    return (
        <>
            <Button className="TableBtn" type="link" onClick={() => setTrue()}>
                {title}
            </Button>
            {ModalState && <ForgeBox Off={setFalse} type={type} data={data} />}
        </>
    )
}

// 通用打开按钮
export const CommonButton = ({ OptionsEvent, type, icon, title, data, danger, other, modal }) => {
    const click = () =>
        OptionsEvent.emit({
            type: data.type,
            data: { title: data.smallTitle || title, data, width: data.width || 1000, other, modal },
        })
    return <ButtonConfig title={title} type={type} icon={icon} click={click} danger={danger} />
}

// 地图定位
export const MapPositioning = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => {
        OptionsEvent.emit({ type: 'MapPositionModal', data: { title, data } })
    }
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 流程图配置
export const FlowChartButton = ({ OptionsEvent, type, icon, title, data }) => {
    const click = () => {
        OptionsEvent.emit({ type: 'FlowChartModal', data: { title, data, width: 1500 } })
    }
    return <ButtonConfig title={title} type={type} icon={icon} click={click} />
}

// 批量操作设置
export const OpenSelection = ({ OptionsEvent, type, icon, title, data, danger }) => {
    const click = () => OptionsEvent.emit({ type: 'openSelection', data })
    return <ButtonConfig title={title} type={type} icon={icon} danger={danger} click={click} />
}
